﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FacialClub.Helpers;
using FacialClub.Models.Entities;
using FacialClub.Models.Interfaces;
using FacialClub.Models.Repositories;
using FacialClub.Models.ViewModels;

namespace FacialClub.Areas.Admin.Controllers
{
    /// <summary>
    /// Payment Method Controller
    /// </summary>
    [Authorize]
    public class PaymentMethodController : Controller
    {
        #region Fields

        private const int DEFAULTPAGESIZE = 10;
        private readonly IPaymentMethodRepository _paymentMethodRepository;

        #endregion

        #region Constructors

        /// <summary>
        /// Dependency Injection enabled constructors
        /// </summary>
        public PaymentMethodController() : this(new PaymentMethodRepository())
        {
        }

        /// <summary>
        /// Payment Method Controller Constructor
        /// </summary>
        /// <param name="paymentMethodRepository"></param>
        public PaymentMethodController(IPaymentMethodRepository paymentMethodRepository)
        {
            _paymentMethodRepository = paymentMethodRepository;
        }

        #endregion

        #region Actions

        /// <summary>
        /// Display the listing of Payment Method
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string strQuery, int? page)
        {
            IQueryable<PaymentMethod> paymentMethod = !string.IsNullOrWhiteSpace(strQuery)
                                                    ? _paymentMethodRepository.FindPaymentMethodByName(strQuery).OrderBy(
                                                        d => d.CreatedDate)
                                                    : _paymentMethodRepository.FindAllPaymentMethod().OrderBy(
                                                        d => d.CreatedDate);
            var paginatedPaymentMethod =
                paymentMethod.ToPagedList(page.HasValue ? page.Value - 1 : 0, DEFAULTPAGESIZE);
            return View(paginatedPaymentMethod);
        }

        /// <summary>
        /// Details of Payment Method
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(int? id)
        {
            if (id != null)
            {
                var paymentMethod = _paymentMethodRepository.GetPaymentMethodById(id.Value);

                if (paymentMethod == null)
                {
                    return new FileNotFoundResult {Message = "No PaymentMethod found for that id"};
                }

                return View(new PaymentMethodViewModel(paymentMethod));
            }

            return new FileNotFoundResult {Message = "No PaymentMethod found due to invalid PaymentMethod id"};
        }

        /// <summary>
        /// Create Payment Method
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult Create()
        {
            var paymentMethod = new PaymentMethod();
            return View(new PaymentMethodViewModel(paymentMethod));
        }

        /// <summary>
        /// Create Payment Method
        /// </summary>
        /// <param name="paymentMethod"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Create(PaymentMethod paymentMethod)
        {
            if (ModelState.IsValid)
            {
                paymentMethod.UserCreated = HttpContext.User.Identity.Name;
                paymentMethod.CreatedDate = DateTime.Now.ToSqlDateTime();
                paymentMethod.IsActive = Convert.ToBoolean(Request["IsActive"]);
                _paymentMethodRepository.Add(paymentMethod);
                _paymentMethodRepository.Save();
                return RedirectToAction("Details", new {id = paymentMethod.PaymentMethodId});
            }

            return View(new PaymentMethodViewModel(paymentMethod));
        }

        /// <summary>
        /// Edit Payment Method
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Edit(int id)
        {
            var paymentMethod = _paymentMethodRepository.GetPaymentMethodById(id);
            return View(new PaymentMethodViewModel(paymentMethod));
        }

        /// <summary>
        /// Edit Payment Method
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var paymentMethod = _paymentMethodRepository.GetPaymentMethodById(id);
            if (ModelState.IsValid)
            {
                try
                {
                    paymentMethod.UserModified = HttpContext.User.Identity.Name;
                    paymentMethod.ModifiedDate = DateTime.Now;
                    UpdateModel(paymentMethod, "PaymentMethod");
                    _paymentMethodRepository.Save();
                    return RedirectToAction("Details", new {id = paymentMethod.PaymentMethodId});
                }
                catch
                {
                    return View(new PaymentMethodViewModel(paymentMethod));
                }
            }
            return View(new PaymentMethodViewModel(paymentMethod));
        }

        /// <summary>
        /// Delete Payment Method
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Delete(int id)
        {
            var paymentMethod = _paymentMethodRepository.GetPaymentMethodById(id);
            return paymentMethod == null ? View("NotFound") : View(new PaymentMethodViewModel(paymentMethod));
        }

        /// <summary>
        /// Delete Payment Method
        /// </summary>
        /// <param name="id"></param>
        /// <param name="confirmButton"></param>
        /// <returns></returns>
        [HttpPost, Authorize]
        public ActionResult Delete(int id, string confirmButton)
        {
            var paymentMethod = _paymentMethodRepository.GetPaymentMethodById(id);

            if (paymentMethod != null)
            {
                _paymentMethodRepository.Delete(paymentMethod);
                _paymentMethodRepository.Save();
                return View("Deleted");
            }
            return View("NotFound");
        }

        /// <summary>
        /// Handle Unknown Action
        /// </summary>
        /// <param name="actionName"></param>
        /// <exception cref="HttpException"></exception>
        protected override void HandleUnknownAction(string actionName)
        {
            throw new HttpException(404, "Action not found");
        }

        #endregion
    }
}